import React from 'react';
import { MemoryRouter } from 'react-router';
import configureStore from 'redux-mock-store';
import { Provider } from 'react-redux';
import List from '@/pages/unitTest/testList';
import { mount } from 'enzyme';
import { initList } from '../../../../mock/unitTest';
// import Date from '../../../mock/Date';

beforeAll(() => {
    jest.mock('../../../mock/Date');
});

const middlewares = [];
const mockStore = configureStore(middlewares);
describe('Test testList render', () => {
    it('render static dom', () => {
        const store = mockStore({
            unitTest: {
                list: [],
            },
            loading: {
                effects: {},
            },
        });
        store.dispatch = jest.fn();
        const wrapper = mount(
            <MemoryRouter>
                <Provider store={store}>
                    <List />
                </Provider>
            </MemoryRouter>,
        );

        const ifRenderList = wrapper.find('List').length;
        expect(ifRenderList).toBe(1);

        const Des = wrapper.find('.des');
        expect(Des.length).toBe(2);

        const Title0 = Des.at(0);
        expect(Title0.text()).toBe('title');

        const Title1 = Des.at(1);
        const titleText0 = Title1.childAt(0);
        expect(titleText0.text()).toBe('title');
        const desText1 = Title1.childAt(1);
        expect(desText1.text()).toBe('11');
        const desText2 = Title1.childAt(2);
        expect(desText2.text()).toBe('22');
    });

    it('render list dom mount', () => {
        const store = mockStore({
            unitTest: {
                list: initList,
            },
            loading: {
                effects: {},
            },
        });
        store.dispatch = jest.fn();
        const wrapper = mount(
            <MemoryRouter>
                <Provider store={store}>
                    <List />
                </Provider>
            </MemoryRouter>,
        );
        const ifRenderDes = wrapper.find('Item').length;
        expect(ifRenderDes).toBe(4);
        expect(store.dispatch).toBeCalledTimes(1);
        expect(store.dispatch).toBeCalledWith({
            type: 'unitTest/requestList',
            payload: {},
        });
    });

    it('dom event', () => {
        const store = mockStore({
            unitTest: {
                list: [],
            },
            loading: {
                effects: {},
            },
        });
        store.dispatch = jest.fn();
        const wrapper = mount(
            <MemoryRouter>
                <Provider store={store}>
                    <List />
                </Provider>
            </MemoryRouter>,
        );
        wrapper
            .find('Button')
            .at(0)
            .simulate('click');
        expect(store.dispatch).toHaveBeenCalledTimes(2);
        expect(store.dispatch).toHaveBeenNthCalledWith(1, {
            type: 'unitTest/requestList',
            payload: {},
        });
        const payload = {
            type: 'unitTest/requestAddItem',
            payload: {
                timeMap: new Date().getTime() / 1000,
            },
        };
        console.log('mock payload', payload);
        expect(store.dispatch).toHaveBeenNthCalledWith(2, payload);
    });
});
